/**
 * 
 */
package org.dragonfire.ti;

import java.util.ArrayList;
import java.util.Arrays;

import org.dragonfire.util.token.Token;
import org.dragonfire.util.token.TokenTable;

/**
 * @author Darth Android
 * @date Nov 10, 2007
 */
public class TIProgram extends VarEntry {

	/**
	 * @param calcType
	 * @param varType
	 * @param varName
	 * @param data
	 */
	public TIProgram(int calcType, int varType, Byte[] varName, Byte[] data) {
		super(calcType, varType, varName, data);
		tt = TIFile.getTokenTable(calcType);
	}

	private TokenTable tt;

	public Token[] getTokens() {
		return tt.readTokens(getData());
	}

	public void setTokens(Token[] tokens) {
		ArrayList<Byte> bytes = new ArrayList<Byte>();
		for (int i = 0; i < tokens.length; i++) {
			bytes.addAll(Arrays.asList(tokens[i].getBytes()));
		}
	}

	public String getDetokenizedData() {
		return Token.toDisplayString(getTokens());
	}

	public String getDetokenizedEditData() {
		return Token.toEditString(getTokens());
	}

	public void setData(String editString) {
		setData(Token.toByteArray(getTokenTable().readTokens(editString)));
	}
}
